# Definition for singly-linked list.

# class ListNode(object):

# def __init__(self, x):

# self.val = x

# self.next = None

class Solution(object):

def mergeTwoLists\(self, l1, l2\):

    lst=ListNode\(0\)

    result=ListNode\(0\)

    result.next=lst



    while \(l1 or l2\):

        if l1 and l2:

            if l1.val<=l2.val:

                lst.next=ListNode\(l1.val\)

                l1=l1.next

                lst=lst.next

            else:

                lst.next=ListNode\(l2.val\)

                l2=l2.next

                lst=lst.next

        elif l1:



            lst.next=ListNode\(l1.val\)

            l1=l1.next

            lst=lst.next

        else :



            lst.next=ListNode\(l2.val\)

            l2=l2.next

            lst=lst.next



    return result.next.next

这个注意点就是一定要单独给个头结点就好了

results matching ""

    No results matching ""